package dao

import (
	"fmt"
	newssql "gitee.com/bingshiwuyu/backend-platform/mysql"
	"gitee.com/bingshiwuyu/trace-source/model"
	"github.com/wonderivan/logger"
)

// DBInt 资源初始化
func DBInt() error {
	db := model.MDBData{
		User:     "root",
		PassWord: "bluesoft",
		Host:     "localhost:3306",
		DB:       "trace",
	}

	DBA := GetDBGroupConfig("trace", db)
	conf := []newssql.SQLGroupConfig{DBA}
	err := newssql.InitSQLClient(conf)
	if err != nil {
		logger.Error("InitSQLClient err:%v", err)
	}

	logger.Debug("InitDB succ")
	return nil
}

// GetDBGroupConfig 初始化数据库信息
func GetDBGroupConfig(name string, db model.MDBData) newssql.SQLGroupConfig {
	dbInfo := fmt.Sprintf("%s:%s@tcp(%s)/%s?"+
		"charset=utf8&max_idle=100&max_active=500&max_lifetime_sec=1800&parseTime=true", db.User, db.PassWord,
		db.Host, db.DB)

	group := newssql.SQLGroupConfig{
		Name:   name,
		Master: dbInfo,
		Slaves: []string{dbInfo},
	}

	return group
}
